using Cysharp.Threading.Tasks;
using MoonFramework.View;
using Tool.Name;

namespace MoonFramework.FSM
{
    public abstract class RunState : BaseState
    {
        protected bool _isRun;
        
        public override void Enter()
        {
            _isRun = true;
            AnimancerManager.Instance.Play(_actor.viewController._RunAnimation, _actor.viewController.AnimancerComponent);
            _actor.viewController.AddAnimationEvent(nameof(FootStep), FootStep);
            Run().Forget();
        }

        protected abstract UniTaskVoid Run();

        public override void Exit()
        {
            _isRun = false;
            _actor.viewController.RemoveAnimationEvent(nameof(FootStep), FootStep);
        }
        
        protected int index;
        //向动画组件发送帧事件
        protected virtual void FootStep()
        {
            index %= 2;
            _actor.viewController.PlayAudio(index == 0? MonsterAudioName.WalkLeft : 
                    MonsterAudioName.WalkRight, 
                0.25f);
            index++;
        }
    }
}